﻿using System;
using UnityEngine;
using Yoozoo.Mars.Got;

namespace Yoozoo.Gameplay.RTS
{
    public class HudEffectPlayer
    {

        private int key;
        private ParticleSystem[] particles;
        private Action<GameObject> loadEffectCallback;
        private Action playFinishCallback;
        private GameObject effect;
        private float lifeTime;
        private int childIndex = -1;

        public void SetChildIndex(int index)
        {
            childIndex = index;
            if (effect != null)
            {
                effect.transform.SetSiblingIndex(index);
            }
        }
        public HudEffectPlayer(int key,Transform parent,Action<GameObject> loadEffectCallback,Action playFinishCallback,float lifeTime)
        {
            childIndex = -1;
            this.key = key;
            this.lifeTime = lifeTime;
            this.loadEffectCallback = loadEffectCallback;
            this.playFinishCallback = playFinishCallback;
            ShowEffect(parent);
        }
        
        

        public void Play()
        {
            if (particles!=null)
            {
                foreach (var particle in particles)
                {
                    particle.Play();
                }
            }
            StartDestroyTimer();
        }

        private void ShowEffect(Transform parent)
        {
            if (effect == null)
            {
                effect = ArmyManager.GetResourceById(key);
                if (effect)
                {
                    effect.SetActive(true);
                    effect.transform.SetParent(parent,true);
                    effect.transform.localEulerAngles = Vector3.zero;
                    if (childIndex >= 0)
                    {
                        effect.transform.SetSiblingIndex(childIndex);
                    }
                    particles = effect.GetComponentsInChildren<ParticleSystem>();
                }
                else
                {
#if DEVELOPMENT_BUILD || UNITY_EDITOR
                    Debug.LogError("ShowReleaseSkillEffect null");
#endif
                }
            }
            else
            {
#if DEVELOPMENT_BUILD || UNITY_EDITOR
                Debug.LogError("ShowReleaseSkillEffect null");
#endif
            }

            if (effect)
            {
                loadEffectCallback.Invoke(effect);
            }

            if (particles!=null)
            {
                foreach (var particle in particles)
                {
                    particle.Play();
                }
            }

            StartDestroyTimer();
        }

        private void StartDestroyTimer()
        {
            StopDestroyTimer();
            Timers.inst.Add(lifeTime,1,RecoverEffectObj);
        }

        private void RecoverEffectObj(object param)
        {
            if (particles != null)
            {
                foreach (var particle in particles)
                {
                    particle.Stop();
                }

                particles = null;
            }

            if (effect!=null)
            {
                RTSLuaCallCSharpManager.RecoverResourceById(key,effect);
                effect = null;
                childIndex = -1;
            }
            Timers.inst.Remove(RecoverEffectObj);
            playFinishCallback?.Invoke();
            playFinishCallback = null;
        }

        private void StopDestroyTimer()
        {
            Timers.inst.Remove(RecoverEffectObj);
        }


        public void Dispose()
        {
            if (effect!=null)
            {
                RTSLuaCallCSharpManager.RecoverResourceById(key,effect);
                effect = null;
            }
            childIndex = -1;
            StopDestroyTimer();
            loadEffectCallback = null;
            playFinishCallback = null;
        }
    }
}
